﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WindowsFormsView;
using WindowsFormsModel.BusinessObjects;

namespace WindowsFormsPresenter.InventoryItem
{
    public class InventoryItemPresenter : Presenter<IInventoryItemView>
    {
        public InventoryItemPresenter(IInventoryItemView view)
            : base(view)
        {
        }

        public void Display(string inventoryItemId)
        {
            if (inventoryItemId == null) return;

            var inventoryItem = Model.GetInventoryItem(inventoryItemId);

            View.InventoryItemID = inventoryItem.InventoryItemID;
            View.InventoryCategoryID = inventoryItem.InventoryCategoryID;
            View.InventoryItemCode = inventoryItem.InventoryItemCode;
            View.InventoryItemName = inventoryItem.InventoryItemName;
            View.UnitID = inventoryItem.UnitID;
            View.UnitPrice = inventoryItem.UnitPrice;
            View.SalesPrice = inventoryItem.SalesPrice;
            View.StockID = inventoryItem.StockID;
            View.MinimumStock = inventoryItem.MinimumStock;
            View.IsActive = inventoryItem.IsActive;
            View.Description = inventoryItem.Description;
            View.RestaurantId = inventoryItem.RestaurantId;
            View.IsMaterial = inventoryItem.IsMaterial;
        }

        public int Save()
        {
            var inventoryItem = new InventoryItemModel
            {
                InventoryItemID = View.InventoryItemID,
                InventoryCategoryID = View.InventoryCategoryID,
                InventoryItemCode = View.InventoryItemCode,
                InventoryItemName = View.InventoryItemName,
                UnitID = View.UnitID,
                UnitPrice = View.UnitPrice,
                SalesPrice = View.SalesPrice,
                StockID = View.StockID,
                MinimumStock = View.MinimumStock,
                IsActive = View.IsActive,
                Description = View.Description,
                RestaurantId = View.RestaurantId,
                IsMaterial = View.IsMaterial
            };

            if (View.InventoryItemID == null)
                return Model.AddInventoryItem(inventoryItem);
            else
                return Model.UpdateInventoryItem(inventoryItem);
        }

        public int Delete(string inventoryItemId)
        {
            return Model.DeleteInventoryItem(inventoryItemId);
        }
    }
}
